//
//  ModuleView.m
//  Game2048
//
//  Created by tarena on 16/3/24.
//  Copyright © 2016年 tarena. All rights reserved.
//

#import "ModuleView.h"

@implementation ModuleView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        moduleWidth = (self.frame.size.width - 60) / 4;
        [self initBackground];
    }
    return self;
}

- (void)initBackground
{
    for (UInt8 row = 0; row < 4; row++)
    {
        for (UInt8 col = 0; col < 4; col++) {
            [self addSubview:[self getModuleRow:row Col:col]];
        }
    }
}


- (UILabel *)getModuleRow:(UInt8)row Col:(UInt8)col
{
    UILabel *module = [[UILabel alloc]initWithFrame:CGRectMake(15+(moduleWidth + 10)*col, 15+(moduleWidth +10)*row, moduleWidth, moduleWidth)];
    module.backgroundColor = [UIColor whiteColor];
    module.clipsToBounds = YES;
    module.layer.cornerRadius = 3;
    return module;
}


- (NSMutableArray *)moduleArray {
    if (!_moduleArray) {
        _moduleArray = [NSMutableArray array];
        for (UInt8 row = 0; row < 4;  row++) {
            NSMutableArray *modules = [NSMutableArray array];
            for (UInt8 col = 0; col < 4; col++) {
                UILabel *module = [self getModuleRow:row Col:col];
                module.textAlignment = NSTextAlignmentCenter;
                module.textColor = [UIColor whiteColor];
                module.font = [UIFont fontWithName:@"Helvetica-Bold" size:22];
                [modules addObject:module];
            }
            [_moduleArray addObject:modules];
        }
    }
    return _moduleArray;
}


- (void)drawRect:(CGRect)rect {
    for (UInt8 row = 0;  row < 4; row++) {
        for (UInt8 col = 0;  col < 4; col++) {
            UILabel *module = self.moduleArray[row][col];
            module.backgroundColor = [self getModuleColor:self.moduleData[row][col]];
            if (self.moduleArray[row][col]) {
                module.text = [NSString stringWithFormat:@"%hu",self.moduleData[row][col]];
            }

            [self addSubview:module];
        }
    }
    
}

- (UIColor *)getModuleColor:(UInt16) data
{
    UIColor *moduleColor = nil;
    switch (data) {
        case 0:
            moduleColor = [UIColor clearColor];
            break;
        case 2:
            //#FFC125
            moduleColor =  [self getColorRed:0xff Green:0xc1 Blue:0x25];
            break;
        case 4:
           // #FF8C00
            moduleColor = [self getColorRed:0xff Green:0x8c Blue:0x00];
            break;
        case 8:
            //#F4A460
            moduleColor = [self getColorRed:0xf4 Green:0xa4 Blue:0x60];
            break;
        case 16:
            //#FF8247
            moduleColor = [self getColorRed:0xff Green:0x82 Blue:0x47];
            break;
        case 32:
            //#EE5C42
            moduleColor = [self getColorRed:0xee Green:0x5c Blue:0x42];
            break;
        case 64:
            //#EE3B3B
            moduleColor = [self getColorRed:0xee Green:0x3b Blue:0x3b];
            break;
        case 128:
            //#EEC900
            moduleColor = [self getColorRed:0xee Green:0xc9 Blue:0x00];
            break;
        case 256:
            //#EEAD0E
            moduleColor = [self getColorRed:0xee Green:0xad Blue:0x0e];
            break;
        case 512:
            //#EE30A7
            moduleColor = [self getColorRed:0xee Green:0x30 Blue:0xA7];
            break;
        case 1024:
//            #EE3A8C
            moduleColor = [self getColorRed:0xee Green:0x3a Blue:0x8c];
            break;
        case 2048:
            //#EE2C2C
            moduleColor = [self getColorRed:0xee Green:0x2c Blue:0x2c];
            break;
        case 4096:
            //#CD661D
            moduleColor = [self getColorRed:0xcd Green:0x66 Blue:0x1d ];
            break;
        case 8192:
            //#CD3333
            moduleColor = [self getColorRed:0xcd Green:0x33 Blue:0x33 ];
            break;
        case 16384:
            //#C71585
            moduleColor = [self getColorRed:0xc7 Green:0x15 Blue:0x85];
            break;
        default:
            //#B03060
            moduleColor = [self getColorRed:0xb0 Green:0x30 Blue:0x60];
            break;
    }
    return moduleColor;
}

- (UIColor *)getModuleColor2:(UInt32)data
{
    UInt8 red = data >> 16 & 0xff0000 ;
    UInt8 green = data >> 8 & 0xff00 ;
    UInt8 blue =  data & 0xff;
    return [[UIColor alloc]initWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
    
}

- (UIColor *)getColorRed:(UInt8)red Green:(UInt8)green Blue:(UInt8)blue
{
    return [[UIColor alloc]initWithRed:red/255.0 green:green/255.0 blue:blue/255.0 alpha:1.0];
}


@end
